package org.ponly.cta4j.factory.gimpy;

import java.awt.image.BufferedImage;
import java.util.Locale;
import java.util.Random;
import org.ponly.cta4j.drawing.Painter;
import org.ponly.cta4j.factory.AbstractCaptchaFactory;

/**
 * Gimpy 验证码工厂.
 *
 * @author vacoor
 * @since 1.0
 */
public class GimpyFactory extends AbstractCaptchaFactory {
    /**
     * random for text length.
     */
    private final Random random = new Random();

    /**
     * 文本生成器.
     */
    private TextGenerator textGenerator;

    /**
     * 是否区分大小写.
     */
    private boolean caseSensitive;

    /**
     * 使用给定的文本生成器创建工厂.
     *
     * @param textGenerator 文本生成器
     * @param caseSensitive 是否大小写敏感
     */
    public GimpyFactory(final TextGenerator textGenerator, final boolean caseSensitive) {
        this.textGenerator = textGenerator;
        this.caseSensitive = caseSensitive;
    }

    /**
     * 使用给定配置构建一个验证码工厂.
     *
     * @param painter       Painter
     * @param textGenerator 文本生成器
     * @param caseSensitive 是否大小写敏感
     */
    public GimpyFactory(final Painter painter, final TextGenerator textGenerator, final boolean caseSensitive) {
        super(painter);
        this.textGenerator = textGenerator;
        this.caseSensitive = caseSensitive;
    }

    /**
     * 使用给定配置构建一个验证码工厂.
     *
     * @param width         验证码宽度
     * @param height        验证码高度
     * @param painter       Painter
     * @param textGenerator 文本生成器
     * @param caseSensitive 是否大小写敏感
     */
    public GimpyFactory(final int width, final int height, final Painter painter,
                        final TextGenerator textGenerator, final boolean caseSensitive) {
        super(width, height, painter);
        this.textGenerator = textGenerator;
        this.caseSensitive = caseSensitive;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Gimpy getCaptcha() {
        return getCaptcha(null);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Gimpy getCaptcha(final Locale locale) {
        final int len = getRandomLength();
        final String text = null != locale ? textGenerator.nextText(len, locale) : textGenerator.nextText(len);
        final BufferedImage canvas = createCanvas();
        final Painter painter = getPainter();

        return new Gimpy(painter.paint(canvas, text), text, isCaseSensitive());
    }

    /**
     * @return 获取随机的文本长度
     */
    protected int getRandomLength() {
        final int min = getPainter().getMinTextLength();
        final int max = getPainter().getMaxTextLength();
        final int range = max - min;
        final int value = 0 < range ? random.nextInt(range) : 0;
        return min + value;
    }

    /**
     * @return 当前使用的文本生成器
     */
    public TextGenerator getTextGenerator() {
        return textGenerator;
    }

    /**
     * 设置当前的文本生成器.
     *
     * @param textGenerator 文本生成器
     */
    public void setTextGenerator(final TextGenerator textGenerator) {
        this.textGenerator = textGenerator;
    }

    /**
     * @return 是否大小写敏感
     */
    public boolean isCaseSensitive() {
        return caseSensitive;
    }

    /**
     * 设置验证码是否大小写敏感.
     *
     * @param caseSensitive 是否大小写敏感
     */
    public void setCaseSensitive(final boolean caseSensitive) {
        this.caseSensitive = caseSensitive;
    }
}
